<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - geometries</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}
		</style>
	</head>
	<body>

		<script src="../build/three.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script src="js/CurveExtras.js"></script>
		<script src="js/ParametricGeometries.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			var container, stats;

			var camera, scene, renderer;

			init();
			animate();

			function init() {

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.y = 400;

				scene = new THREE.Scene();

				var light, object, materials;

				scene.add( new THREE.AmbientLight( 0x404040 ) );

				light = new THREE.DirectionalLight( 0xffffff );
				light.position.set( 0, 0, 1 );
				scene.add( light );

				var map = new THREE.TextureLoader().load( 'textures/UV_Grid_Sm.jpg' );
				map.wrapS = map.wrapT = THREE.RepeatWrapping;
				map.anisotropy = 16;

				materials = [
					new THREE.MeshLambertMaterial( { map: map, side: THREE.DoubleSide } ),
					new THREE.MeshBasicMaterial( { color: 0xffffff, wireframe: true, transparent: true, opacity: 0.1, side: THREE.DoubleSide } )
				];

				var p = 2;
				var q = 3;
				var radius = 150, tube = 10, segmentsT = 50, segmentsR = 20;

				var GrannyKnot =  new THREE.Curves.GrannyKnot();

				var torus2 = new THREE.ParametricGeometries.TorusKnotGeometry( radius, tube, segmentsT, segmentsR, p , q );
				var sphere2 = new THREE.ParametricGeometries.SphereGeometry( 75, 20, 10 );
				var tube2 = new THREE.ParametricGeometries.TubeGeometry( GrannyKnot, 150, 2, 8, true, false );

				var geo;

				// Klein Bottle

				geo = new THREE.ParametricBufferGeometry( THREE.ParametricGeometries.klein, 20, 20 );
				object = THREE.SceneUtils.createMultiMaterialObject( geo, materials );
				object.position.set( 0, 0, 0 );
				object.scale.multiplyScalar( 10 );
				scene.add( object );

				// Mobius Strip

				geo = new THREE.ParametricBufferGeometry( THREE.ParametricGeometries.mobius, 20, 20 );
				object = THREE.SceneUtils.createMultiMaterialObject( geo, materials );
				object.position.set( 10, 0, 0 );
				object.scale.multiplyScalar( 100 );
				scene.add( object );

				// Plane

				geo = new THREE.ParametricBufferGeometry( THREE.ParametricGeometries.plane( 200, 200 ), 10, 20 );
				object = THREE.SceneUtils.createMultiMaterialObject( geo, materials );
				object.position.set( 0, 0, 0 );
				scene.add( object );

				object = THREE.SceneUtils.createMultiMaterialObject( torus2, materials );
				object.position.set( 0, 100, 0 );
				scene.add( object );

				object = THREE.SceneUtils.createMultiMaterialObject( sphere2, materials );
				object.position.set( 200, 0, 0 );
				scene.add( object );

				object = THREE.SceneUtils.createMultiMaterialObject( tube2, materials );
				object.position.set( 100, 0, 0 );
				scene.add( object );

				object = new THREE.AxisHelper( 50 );
				object.position.set( 200, 0, -200 );
				scene.add( object );

				renderer = new THREE.WebGLRenderer( { antialias: true } );
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				container.appendChild( renderer.domElement );

				stats = new Stats();
				container.appendChild( stats.dom );

				window.addEventListener( 'resize', onWindowResize, false );

			}

			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			function animate() {

				requestAnimationFrame( animate );

				render();
				stats.update();

			}

			function render() {

				var timer = Date.now() * 0.0001;

				camera.position.x = Math.cos( timer ) * 800;
				camera.position.z = Math.sin( timer ) * 800;

				camera.lookAt( scene.position );

				for ( var i = 0, l = scene.children.length; i < l; i ++ ) {

					var object = scene.children[ i ];

					object.rotation.x = timer * 5;
					object.rotation.y = timer * 2.5;

				}

				renderer.render( scene, camera );

			}

		</script>

	</body>
</html>
